package com.tj.jinzao.presenter;

import android.text.TextUtils;

import com.tj.jinzao.App;
import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.base.BasePresenter;
import com.tj.jinzao.callback.IListView;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.Article;
import com.tj.jinzao.model.ResultResponse;

import java.util.HashMap;
import java.util.List;

/**
 * 日记，文章列表处理
 * Created by wjy 2018/12/10.
 */
public class ArticlePresenter extends BasePresenter<IListView<List<Article>>> {

    public ArticlePresenter(IListView<List<Article>> view) {
        super(view);
    }

    public void getArticleList(HashMap<String, Object> map, boolean needCity) {
        long uId = UserManager.getInstance().getUser().getId();
        map.put("loginUid", uId);
        if (needCity) {
            if (App.sCity != null && !TextUtils.equals(App.sCity.getName(), "全国城市") && !TextUtils.isEmpty(App.sCity.getName())) {
                map.put("city", App.sCity.getName());
            }else {
                map.put("city","");
            }
        }
        addSubscription(mApiService.getArticleList(map), new SubscriberCallBack<List<Article>>(mView.getContext()) {
            @Override
            protected void onSuccess(List<Article> list) {
                if (mView != null) mView.onRequestSuccess(list);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }

            @Override
            protected boolean isShowProgress() {
                return false;
            }

            @Override
            protected void onPageSuccess(int currentPage, int allPage, int allSize) {
                if (mView != null) mView.onRequestSuccessPage(currentPage, allPage, allSize);
            }
        });
    }

    public void getMyFollowArticleList(int page) {
        long uId = UserManager.getInstance().getUser().getId();
        addSubscription(mApiService.getMyFollowArticleList(page, uId), new SubscriberCallBack<List<Article>>(mView.getContext()) {
            @Override
            protected void onSuccess(List<Article> list) {
                if (mView != null) mView.onRequestSuccess(list);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }

            @Override
            protected boolean isShowProgress() {
                return false;
            }
        });
    }

    public void followUser(final long followId) {
        long uId = UserManager.getInstance().getUser().getId();
        addSubscription(mApiService.praise(uId, followId, 3), new SubscriberCallBack<String>(mView.getContext()) {
            @Override
            protected void onSuccess(String success) {
                if (mView != null) mView.onReload(followId);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }

            @Override
            protected boolean isShowProgress() {
                return true;
            }
        });
    }

    public void getMyCollectArticleList(HashMap<String, Object> map) {
        long uId = UserManager.getInstance().getUser().getId();
        map.put("loginUid", uId);
        addSubscription(mApiService.getMyCollectArticleList(map), new SubscriberCallBack<List<Article>>(mView.getContext()) {
            @Override
            protected void onSuccess(List<Article> list) {
                if (mView != null) mView.onRequestSuccess(list);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }

            @Override
            protected boolean isShowProgress() {
                return false;
            }
        });
    }

    public void searchArticle(int page, String keyword) {
        addSubscription(mApiService.searchArticle(page, keyword), new SubscriberCallBack<List<Article>>(mView.getContext()) {
            @Override
            protected void onSuccess(List<Article> list) {
                if (mView != null) mView.onRequestSuccess(list);
            }

            @Override
            protected void onFailure(ResultResponse response) {
                if (mView != null) mView.onRequestError(response.getMessage());
            }

            @Override
            protected boolean isShowProgress() {
                return false;
            }
        });
    }

}
